using System;
using System.Drawing;

namespace Meta.Basic2D {
	/// <summary>
	/// Summary description for Noise.
	/// </summary>
	public class Noise {
		const int arraySize=256;
		const int arrayMask=arraySize-1;
		static byte[,] noiseArray = new byte[arraySize, arraySize];
		static Noise() {
			System.Random r = new System.Random(123456789);
			for (int i=0; i<arraySize; i++)
				for (int j=0; j<arraySize; j++)
					noiseArray[i, j]=(byte)r.Next(255);
		}

		static public float Noise2D(float x, float y) {
			int gridX = (int)x;
			int gridY = (int)y;
			float deltaX = x-gridX;
			float deltaY = y-gridY;
			float minusDeltaX = 1-deltaX;
			float minusDeltaY = 1-deltaY;

			return minusDeltaX*minusDeltaY*noiseArray[gridX%arrayMask,gridY%arrayMask]
				+minusDeltaX*deltaY*noiseArray[gridX%arrayMask,(gridY+1)%arrayMask]
				+deltaX*minusDeltaY*noiseArray[(gridX+1)%arrayMask,gridY%arrayMask]
				+deltaX*deltaY*noiseArray[(gridX+1)%arrayMask,(gridY+1)%arrayMask];
		}

		static public float SignedNoise2D(float x, float y) {
			int gridX = (int)x;
			int gridY = (int)y;
			float deltaX = x-gridX;
			float deltaY = y-gridY;
			float minusDeltaX = 1-deltaX;
			float minusDeltaY = 1-deltaY;

			return (minusDeltaX*minusDeltaY*noiseArray[gridX%arrayMask,gridY%arrayMask]
				+minusDeltaX*deltaY*noiseArray[gridX%arrayMask,(gridY+1)%arrayMask]
				+deltaX*minusDeltaY*noiseArray[(gridX+1)%arrayMask,gridY%arrayMask]
				+deltaX*deltaY*noiseArray[(gridX+1)%arrayMask,(gridY+1)%arrayMask]-127.5F)/127.5F;
		}

		static public float Noise2D(object o) {
			PointF p = Meta.PointFVectorSpace.ToPointF(o, "noise");
			return Noise2D(p.X, p.Y);
		}
	}

	class PerlinNoise {
		// Perlin Noise code, adapted from Paul Bourke's adaptation of Ken's original C code.
		// taken from http://astronomy.swin.edu.au/~pbourke/texture/perlin/
		const int B = 0x100;
		const int BM = 0xff;
		const int N = 0x1000;
		const int NP = 12;   // 2^N
		const int NM = 0xfff;

		static float s_curve(float t) {
			return t * t * (3.0F - 2.0F * t);
		}

		static float lerp(float t, float a, float b) {
			return a + t * (b - a);
		}

		static void setup(float f, out float t, out int b0, out int b1, out float r0, out float r1) {
			t = f + N;
			b0 = ((int)t) & BM;
			b1 = (b0+1) & BM;
			r0 = t - (int)t;
			r1 = r0 - 1.0F;
		}

		static float at2(float[] q, float rx, float ry) {
			return rx * q[0] + ry * q[1];
		}
		static float at3(float[] q, float rx, float ry, float rz) {
			return rx * q[0] + ry * q[1] + rz * q[2];
		}

		static System.Random randomObject = new Random(123456789);
		static int random() {
			return randomObject.Next();
		}

		// Coherent noise function over 1, 2 or 3 dimensions
		// (copyright Ken Perlin)

		static int[] p = new int[B + B + 2];
		static float[][] g3 = new float [B + B + 2][];
		static float[][] g2= new float[B + B + 2][];
		static float[] g1= new float[B + B + 2];

		public static float Noise(object o) {
			if (Meta.Utilities.IsNumber(o))
				return noise1(Convert.ToSingle(o));
			else
				return noise2(Meta.PointFVectorSpace.ToPointF(o, "noise"));
		}

		public static float noise1(float arg) {
			int bx0, bx1;
			float rx0, rx1, sx, t, u, v;

			//vec[0] = arg;

			setup(arg, out t, out bx0, out bx1, out rx0, out rx1);

			sx = s_curve(rx0);
			u = rx0 * g1[ p[ bx0 ] ];
			v = rx1 * g1[ p[ bx1 ] ];

			return(lerp(sx, u, v));
		}

		static float noise2(System.Drawing.PointF vec) {
			int bx0, bx1, by0, by1, b00, b10, b01, b11;
			float rx0, rx1, ry0, ry1, sx, sy, a, b, t, u, v;
			float[] q;
			int i, j;

			setup(vec.X, out t, out bx0, out bx1, out rx0, out rx1);
			setup(vec.Y, out t, out by0, out by1, out ry0, out ry1);

			i = p[ bx0 ];
			j = p[ bx1 ];

			b00 = p[ i + by0 ];
			b10 = p[ j + by0 ];
			b01 = p[ i + by1 ];
			b11 = p[ j + by1 ];

			sx = s_curve(rx0);
			sy = s_curve(ry0);

			q = g2[ b00 ] ; u = at2(q, rx0,ry0);
			q = g2[ b10 ] ; v = at2(q, rx1,ry0);
			a = lerp(sx, u, v);

			q = g2[ b01 ] ; u = at2(q, rx0,ry1);
			q = g2[ b11 ] ; v = at2(q, rx1,ry1);
			b = lerp(sx, u, v);

			return lerp(sy, a, b);
		}

		static float noise3(float[] vec3) {
			int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
			float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
			float[] q;
			int i, j;

			setup(vec3[0], out t, out bx0,out bx1, out rx0, out rx1);
			setup(vec3[1], out t, out by0, out by1, out ry0,out ry1);
			setup(vec3[2], out t, out bz0, out bz1, out rz0, out rz1);

			i = p[ bx0 ];
			j = p[ bx1 ];

			b00 = p[ i + by0 ];
			b10 = p[ j + by0 ];
			b01 = p[ i + by1 ];
			b11 = p[ j + by1 ];

			t  = s_curve(rx0);
			sy = s_curve(ry0);
			sz = s_curve(rz0);

			q = g3[ b00 + bz0 ] ; u = at3(q, rx0,ry0,rz0);
			q = g3[ b10 + bz0 ] ; v = at3(q, rx1,ry0,rz0);
			a = lerp(t, u, v);

			q = g3[ b01 + bz0 ] ; u = at3(q, rx0,ry1,rz0);
			q = g3[ b11 + bz0 ] ; v = at3(q, rx1,ry1,rz0);
			b = lerp(t, u, v);

			c = lerp(sy, a, b);

			q = g3[ b00 + bz1 ] ; u = at3(q, rx0,ry0,rz1);
			q = g3[ b10 + bz1 ] ; v = at3(q, rx1,ry0,rz1);
			a = lerp(t, u, v);

			q = g3[ b01 + bz1 ] ; u = at3(q, rx0,ry1,rz1);
			q = g3[ b11 + bz1 ] ; v = at3(q, rx1,ry1,rz1);
			b = lerp(t, u, v);

			d = lerp(sy, a, b);

			return lerp(sz, c, d);
		}

		static void normalize2(float[] v) {
			float s;

			s = (float)Math.Sqrt(v[0] * v[0] + v[1] * v[1]);
			v[0] = v[0] / s;
			v[1] = v[1] / s;
		}

		static void normalize3(float[] v) {
			float s;

			s = (float)Math.Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
			v[0] = v[0] / s;
			v[1] = v[1] / s;
			v[2] = v[2] / s;
		}

		static PerlinNoise() {
			int i, j, k;

			for (i=0; i<g2.Length; i++)
				g2[i] = new float[2];
			for (i=0; i<g3.Length; i++)
				g3[i] = new float[3];

			for (i = 0 ; i < B ; i++) {
				p[i] = i;
				g1[i] = (float)((random() % (B + B)) - B) / B;

				for (j = 0 ; j < 2 ; j++)
					g2[i][j] = (float)((random() % (B + B)) - B) / B;
				normalize2(g2[i]);

				for (j = 0 ; j < 3 ; j++)
					g3[i][j] = (float)((random() % (B + B)) - B) / B;
				normalize3(g3[i]);
			}

			while (--i!=0) {
				k = p[i];
				p[i] = p[j = random() % B];
				p[j] = k;
			}

			for (i = 0 ; i < B + 2 ; i++) {
				p[B + i] = p[i];
				g1[B + i] = g1[i];
				for (j = 0 ; j < 2 ; j++)
					g2[B + i][j] = g2[i][j];
				for (j = 0 ; j < 3 ; j++)
					g3[B + i][j] = g3[i][j];
			}
		}

		// --- My harmonic summing functions - PDB --------------------------

		//
		//   In what follows "alpha" is the weight when the sum is formed.
		//   Typically it is 2, As this approaches 1 the function is noisier.
		//   "beta" is the harmonic scaling/spacing, typically 2.
		//

		public static float Turbulence(object o, float alpha, float beta, int n) {
			if (Meta.Utilities.IsNumber(o))
				return PerlinNoise1D(Convert.ToSingle(o), alpha, beta, n);
			else
				return PerlinNoise2D(Meta.PointFVectorSpace.ToPointF(o, "noise"), alpha, beta, n);
		}

		static float PerlinNoise1D(float x,float alpha,float beta,int n) {
			int i;
			float val,sum = 0;
			float p,scale = 1;

			p = x;
			for (i=0;i<n;i++) {
				val = noise1(p);
				sum += val / scale;
				scale *= alpha;
				p *= beta;
			}
			return(sum);
		}

		static float PerlinNoise2D(PointF p,float alpha,float beta,int n) {
			int i;
			float val,sum = 0;
			float scale = 1;

			for (i=0;i<n;i++) {
				val = noise2(p);
				sum += val / scale;
				scale *= alpha;
				p.X *= beta;
				p.Y *= beta;
			}
			return(sum);
		}

		static float PerlinNoise3D(float x,float y,float z,float alpha,float beta,int n) {
			int i;
			float val,sum = 0;
			float[] p = new float[3];
			float scale = 1;

			p[0] = x;
			p[1] = y;
			p[2] = z;
			for (i=0;i<n;i++) {
				val = noise3(p);
				sum += val / scale;
				scale *= alpha;
				p[0] *= beta;
				p[1] *= beta;
				p[2] *= beta;
			}
			return(sum);
		}
	}
}